layui.config({
    base: '../../layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'table'], function(){
    var $ = layui.$
    ,form = layui.form
    ,table = layui.table
    ,admin = layui.admin;
    
     //表格管理
  table.render({
    elem: '#LAY-user-back-dict'
    ,url: "/api/admin/dict/page" //模拟接口
    ,cols: [[
      {type: 'numbers',title: '序号', width: 80,fixed: 'left'}
      ,{field: 'name', title: '名称'}
      ,{field: 'type', title: '类型字典',templet: '#buttonTpl'}
      ,{field: 'typeCode', title: '类型编码'}
      ,{field: 'system', title: '系统数据', templet: '#buttonTp2'}
      ,{field: 'remarks', title: '详细描述'}
      ,{title: '操作', width: 210, align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin'}
    ]]
    ,page: true
  });

   //监听工具条
   table.on('tool(LAY-user-back-dict)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      active.del(data.id);
    } else if(obj.event === 'edit'){
      active.edit(data.id);
    } else if(obj.event === 'add'){
      active.add();
    } else if(obj.event === 'setting'){
      active.setting(data.typeCode, data.type);
    }
  });
  
  $('#add').click(function () {
    active.add();
  });

    //搜索角色
    form.on('submit(LAY-user-back-dict_find)', function(data){
      //执行重载
      table.reload('LAY-user-back-dict', {
        where: data.field
      });
    });
  
    //事件
    var active = {
      del: function(id){   
        layer.confirm('确定删除吗？', function(index) {
            delData(id, index);
        });
      },
      add: function(){
        updateOrAdd("添加字典", "");
      },
      edit:function(id){
        updateOrAdd("编辑字典", id);
      },
      setting:function(typeCode, type){
        if(type == 0){
          // list 字典
          findDicts(typeCode);
        } else {
          findTreeDicts(typeCode);
        }
        
      }
    }  
  
   
    function updateOrAdd(title, id){
      layer.open({
        type: 2
        ,title: title
        ,content: 'dictForm.html#/id='+id
        ,area: ['500px', '480px']
        ,btn: ['确定', '取消']
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
          ,submit = layero.find('iframe').contents().find("#LAY-dict-submit");

          //监听提交
          iframeWindow.layui.form.on('submit(LAY-dict-submit)', function(data){
            var field = data.field; //获取提交的字段
            //提交 Ajax 成功后，静态更新表格中的数据
           if(field.id != undefined && field.id != ""){
              subUpdateOrSave(field,"PUT", index);
           } else{
              delete field.id;
              subUpdateOrSave(field,"POST", index);
           }
           
          });  
          
          submit.trigger('click');
        }
        ,success: function(layero, index){
        
        }
      })
    }



    //修改或者添加
    function subUpdateOrSave(data, method, index){
      layer.close(index); //关闭弹层
      data._method =method;
      admin.req({
        type: method,
        url:"/api/admin/dict",  
        data: JSON.stringify(data),   
        dataType:"json",
        contentType:'application/json;charset=utf8',
        success:function(res){
            // 表示是修改数据
            var data = res.data;
            if(res.code==200){
              table.reload('LAY-user-back-dict'); //数据刷新
            }
        },
        error:function(jqXHR){
            alert("发生错误："+ jqXHR.status);
        }
      });
    }

    function delData(id, index){
      layer.close(index); //关闭弹层
      admin.req({
        type: "DELETE",
        url:"/api/admin/dict/"+id,  
        dataType:"json",
        contentType:'application/json;charset=utf8',
        success:function(res){
            var data = res.data;
            if(res.code==200){
              table.reload('LAY-user-back-dict'); //数据刷新
            }
        },
        error:function(jqXHR){
            alert("发生错误："+ jqXHR.status);
        }
      });
    }

    // 打开list字典
    function findDicts(typeCode){
      var index =layer.open({
        type: 2
        ,title: "字典配置"
        ,content: 'dictListList.html#/typeCode='+typeCode
      })
      layer.full(index);
    }

    // 打开树字典
    function findTreeDicts(typeCode){
      var index =layer.open({
        type: 2
        ,title: "字典配置"
        ,content: 'dictTreeList.html#/typeCode='+typeCode
      })
      layer.full(index);
    }

  });